import $ from "jquery";
import ViewContainer from "./view_container";
import MotionLayer from "./motion_layer";
import EventCenter from "./event_center";
import Event from "./event";

export default class Motion extends ViewContainer {
  clone() {
    let e = this,
      t = new Motion();
    return e.copyProperties(t), t;
  }
  copyProperties(e) {
    let t = this;
    ViewContainer.prototype.copyProperties.call(t, e), (e.layers = []);
    for (let r = 0; r < t.layers.length; r++) {
      let i = t.layers[r];
      e.layers.push(i.clone());
    }
    (e._duration = t.duration()),
      e.loop(t.loop()),
      e.currentTime(t.currentTime()),
      e.updateFromTime(),
      e.play();
  }
  updatePropsFromXML(t) {
    let i = this;
    ViewContainer.prototype.updatePropsFromXML.call(i, t), (i.layers = []);
    let s = t.find("> layer");
    s.each(function (t, n) {
      let s = $(n),
        o = new MotionLayer();
      o.updateFromXML(s), i.layers.push(o);
    }),
      (i._duration = parseFloat(t.attr("duration"))),
      i.loop(t.is("[loop]") ? t.attr("loop") == "true" : !0),
      t.is("[reverse]") && i.reverse(),
      t.is("[time]")
        ? i.currentTime(parseFloat(t.attr("time")))
        : i.currentTime(0),
      i.updateFromTime(),
      t.is("[auto-play]")
        ? t.attr("auto-play") == "true"
          ? i.play()
          : i.pause()
        : i.play();
  }
  layers = null;
  layerContentByName(e) {
    let t = this,
      n = null;
    for (let r = 0; r < t.layers.length; r++) {
      let i = t.layers[r].contentView;
      if (i.name() == e) {
        n = i;
        break;
      }
    }
    return n;
  }
  isReverse = !1;
  reverse() {
    let e = this;
    for (let t = 0; t < e.layers.length; t++) {
      let n = e.layers[t];
      n.reverse();
    }
    (e.currentTimeChanged = !0),
      e.updateFromTime(),
      (e.isReverse = !e.isReverse);
  }
  _duration = 0;
  duration() {
    let e = this;
    return e._duration;
  }
  currentTimeChanged = !0;
  _currentTime = 0;
  currentTime(e?) {
    let t = this;
    return (
      isNaN(e) == 0 &&
        t._currentTime != e &&
        (t.loop()
          ? (e -= Math.floor(e / t.duration()) * t.duration())
          : (e = Math.min(t.duration(), Math.max(0, e))),
        t._currentTime != e &&
          ((t._currentTime = e),
          (t.currentTimeChanged = !0),
          t.updateFromTime())),
      t._currentTime
    );
  }
  isPlaying = !1;
  pause() {
    let e = this;
    e.isPlaying = !1;
  }
  play(e?) {
    let t = this;
    t.currentTime(e), (t.isPlaying = !0);
  }
  _loop = !1;
  loop(e?) {
    let t = this;
    return (
      typeof e == "boolean" &&
        e != t._loop &&
        ((t._loop = e), t.needToRender(!0)),
      t._loop
    );
  }
  updateFromTime() {
    let e = this;
    if (e.currentTimeChanged) {
      (e.currentTimeChanged = !1), e.empty();
      for (let t = 0; t < e.layers.length; t++) {
        let n = e.layers[t].contentWithTime(e.currentTime());
        n != null && e.addSubView(n);
      }
    }
  }
  update(e, t) {
    let r = this;
    ViewContainer.prototype.update.call(r, e, t);
    if (r.isPlaying) {
      let o = r.currentTime() + t;
      r.currentTime(r.currentTime() + t),
        o >= r.duration() &&
          (r.loop() == 0 && r.pause(), EventCenter.dispatchEvent(new Event(r, Event.END)));
    }
  }
  dealloc() {
    let e = this;
    for (let t = 0; t < e.layers.length; t++) e.layers[t].dealloc();
    EventCenter.removeListener(e), ViewContainer.prototype.dealloc.call(e);
  }
}
